package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.FpShopActivityUserDTO;
import com.financing.wap.service.IFpShopActivityUserService;
import com.financing.wap.web.controller.BaseController;

@Controller("fpShopActivityUserController")
@RequestMapping("/api/fpShopActivityUser")
public class FpShopActivityUserController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopActivityUserService fpShopActivityUserService;
	
	@RequestMapping("/listFpShopActivityUser")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopActivityUserDTO fpShopActivityUser,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopActivityUser list begin .......");
			String winner = request.getParameter("winner");
			if(StringUtils.isNotBlank(winner)){
				fpShopActivityUser.setWinner("1,2,3");
			}
			
			int count = fpShopActivityUserService.queryListCount(fpShopActivityUser);
			
			pager.setTotalCount(count);
			
			List<FpShopActivityUserDTO> fpShopActivityUserList = null;
			
			if(count > 0){
				fpShopActivityUserList = fpShopActivityUserService.queryList(fpShopActivityUser);
			}else{
				fpShopActivityUserList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopActivityUserList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopActivityUser list end .......");
		}catch(Exception e){
			log.error("query fpShopActivityUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopActivityUser")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopActivityUserDTO fpShopActivityUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopActivityUser  begin .......");
			
			
			int id = fpShopActivityUserService.insert(fpShopActivityUser);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopActivityUser list end .......");
		}catch(Exception e){
			log.error("add fpShopActivityUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopActivityUser")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopActivityUserDTO fpShopActivityUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopActivityUser  begin .......");
			
			Integer id = fpShopActivityUser.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopActivityUserDTO dto = fpShopActivityUserService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopActivityUser  end .......");
		}catch(Exception e){
			log.error("query detail fpShopActivityUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopActivityUser")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopActivityUserDTO fpShopActivityUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopActivityUser  begin .......");
			
			Integer id = fpShopActivityUser.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpShopActivityUserService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopActivityUser  end .......");
		}catch(Exception e){
			log.error("delete fpShopActivityUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopActivityUser")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopActivityUserDTO fpShopActivityUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopActivityUser  begin .......");
			
			Integer id = fpShopActivityUser.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			fpShopActivityUserService.update(fpShopActivityUser);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopActivityUser list end .......");
		}catch(Exception e){
			log.error("update fpShopActivityUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopActivityUser")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopActivityUserDTO fpShopActivityUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopActivityUser  begin .......");
			
			Integer id = fpShopActivityUser.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			fpShopActivityUserService.updateForce(fpShopActivityUser);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopActivityUser list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopActivityUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
